# !/usr/bin/env python3
# -*- coding: utf-8 -*-

'此模块负责生成web文件'

import fs

from crown.leaf import copy_dist, folders, template_dict
from html_minifier.minify import Minifier


def fruit(*templates) -> None:
    """文件写入

    拷贝文件至目标文件，将渲染的html、js字符文件写入本地，并对html文件进行压缩

    Args:
        templates: tuple对象，待处理的模板数据

    Returns:
        None

    Raises:
        None
        """
    osfs = fs.open_fs('./')

    # 检查并创建目标文件夹
    for p in folders:
        if osfs.exists(p):
            pass
        else:
            osfs.makedirs(p)

    # 将图片文件拷贝到目标文件夹
    for p in osfs.walk.files(filter=['*.jpg']):
        if (dirname := fs.path.dirname(p)): # noqa E203 E231 
            pass
    fs.copy.copy_dir(osfs, dirname, osfs, '{}/'.format(folders[1]))

    # 将templates/common/、templates/styles/下文件拷贝到目标文件夹下
    for v in copy_dist['from']:
        fs.copy.copy_dir(osfs, v, osfs, copy_dist['to'])

    # 压缩html文件，并将html、js文件写入目标文件夹
    html_minifier = None
    for i, _ in enumerate(templates):
        if templates[i] is None:
            pass
        else:
            if '/>' in templates[i]:
                html_minifier = Minifier(templates[i])
                osfs.writetext(template_dict['to'][i], html_minifier.minify())
            else:
                osfs.writetext(template_dict['to'][i], templates[i])
